#!/usr/bin/env python
# -*- coding:utf-8 -*-
# @FileName  :dic_sort_42.py
# @Time      :2020/6/23 17:39
# @Author    :Administrator


# 声明字典
key_value = {}
# 初始化
key_value[2] = 56
key_value[1] = 2
key_value[5] = 12
key_value[4] = 24
key_value[6] = 18
key_value[3] = 323


def dictionary():
    print('按键(key)排序：')

    # sorted(key_value) 返回一个迭代器
    # 字典按键排序
    for i in sorted(key_value):
        print((i, key_value[i]), end="")


def dictionary2():
    print('按值(value)排序:')
    print(sorted(key_value.items(), key=lambda kv: (kv[1], kv[0])))




def main():
    dictionary()
    dictionary2()

main()
